<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>spring面试 | 雁过留声</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="spring ioc容器初始化过程  一个http请求的处理  自定义一个springboot-starter  分三步  pom.xml 文件中引入 spring-boot-autoconfigure   meta-inf 创建spring.factories 并配置EnableAutoConfiguration 路径  创建需要自动装配的类，在配置类中可以注册其他需要的service Bea">
<meta property="og:type" content="article">
<meta property="og:title" content="spring面试">
<meta property="og:url" content="http://yanglf007.gitee.io/blog/2020/08/09/spring%E9%9D%A2%E8%AF%95/index.html">
<meta property="og:site_name" content="雁过留声">
<meta property="og:description" content="spring ioc容器初始化过程  一个http请求的处理  自定义一个springboot-starter  分三步  pom.xml 文件中引入 spring-boot-autoconfigure   meta-inf 创建spring.factories 并配置EnableAutoConfiguration 路径  创建需要自动装配的类，在配置类中可以注册其他需要的service Bea">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-09T05:23:04.017Z">
<meta property="article:modified_time" content="2020-09-24T16:41:06.445Z">
<meta property="article:author" content="yanglf007">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog/atom.xml" title="雁过留声" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/blog/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">雁过留声</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yanglf007.gitee.io/blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-spring面试" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/blog/2020/08/09/spring%E9%9D%A2%E8%AF%95/" class="article-date">
  <time datetime="2020-08-09T05:23:04.017Z" itemprop="datePublished">2020-08-09</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      spring面试
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <ol>
<li><p>spring ioc容器初始化过程</p>
</li>
<li><p>一个http请求的处理</p>
</li>
<li><p>自定义一个springboot-starter</p>
<p> 分三步</p>
<ol>
<li><p>pom.xml 文件中引入 spring-boot-autoconfigure </p>
</li>
<li><p>meta-inf 创建spring.factories 并配置EnableAutoConfiguration 路径</p>
</li>
<li><p>创建需要自动装配的类，在配置类中可以注册其他需要的service Bean</p>
<p>原理：</p>
</li>
<li><p>@springbootApplication 注解中包含一个@EnableAutoConfiguration注解，</p>
</li>
<li><p>@EnableAutoConfiguration 注解会用@Import 注入一个AutoConfigurationImportSelector 类</p>
</li>
<li><p>importSelector 类中有个方法selectImports（），会调用getAutoConfigurationEntry（），这个类会获取所有配置的enableAutoConfiguration类，<br>然后注入到spring容器中</p>
</li>
</ol>
</li>
<li><p>autowaried自动注入原理</p>
</li>
</ol>
<p>5.@springbootApplication 注解作用<br>该注解主要包含三个注解 @ComponentScan，@SpringBootConfiguration，@EnableAutoConfiguration<br>@ComponentScan 扫描启动类下包和子包具有 @Service,@Component,@Controller注解的类<br>@SpringBootConfiguration 表明启动类是一个配置类，可以在里面注册bean<br>@EnableAutoConfiguration 扫描所在的类 一般会在自定义starter中在spring.factories配置</p>
<ol start="6">
<li>spring 容器初始化过程</li>
</ol>
<p>public void refresh() throws BeansException, IllegalStateException {<br>synchronized (this.startupShutdownMonitor) {<br>// 1. 初始化 refresh 的上下文环境<br>this.prepareRefresh();</p>
<p>// 2. 初始化 BeanFactory，加载并解析配置<br>ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();</p>
<p>/* —至此，已经完成了简单容器的所有功能，下面开始对简单容器进行增强— */</p>
<p>// 3. 对 BeanFactory 进行功能增强<br>this.prepareBeanFactory(beanFactory);</p>
<p>try {<br>// 4. 后置处理 beanFactory，交由子类实现<br>this.postProcessBeanFactory(beanFactory);</p>
<p>// 5. 调用已注册的 BeanFactoryPostProcessor<br>this.invokeBeanFactoryPostProcessors(beanFactory);</p>
<p>// 6. 注册 BeanPostProcessor，仅仅是注册，调用在getBean的时候<br>this.registerBeanPostProcessors(beanFactory);</p>
<p>// 7. 初始化国际化资源<br>this.initMessageSource();</p>
<p>// 8. 初始化事件广播器<br>this.initApplicationEventMulticaster();</p>
<p>// 9. 留给子类实现的模板方法<br>this.onRefresh();</p>
<p>// 10. 注册事件监听器<br>this.registerListeners();</p>
<p>// 11. 实例化所有非延迟加载的单例<br>this.finishBeanFactoryInitialization(beanFactory);</p>
<p>// 12. 完成刷新过程，发布应用事件<br>this.finishRefresh();</p>
<p>springbean 生命周期</p>
<p>InstantiationAwareBeanPostProcessor createBean<br>一. 实例化<br>从xml读取bean配置到Resource中,将resource解析成BeanDefinition<br>利用构造方法和反射,实例化bean,并包装成BeanWrapper,</p>
<p>二. 属性赋值</p>
<p>三. 初始化</p>
<p>四. 销毁</p>
<p>spring 三级缓存<br><a href="https://www.jianshu.com/p/ed58e788639d" target="_blank" rel="noopener">https://www.jianshu.com/p/ed58e788639d</a></p>
<p>aop原理<br>org.springframework.aop.framework.JdkDynamicAopProxy</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yanglf007.gitee.io/blog/2020/08/09/spring%E9%9D%A2%E8%AF%95/" data-id="ckgtnonn5000nm8ularu3ftlz" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2020/08/09/mybatis%E9%9D%A2%E8%AF%95/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          mybatis面试
        
      </div>
    </a>
  
  
    <a href="/blog/2020/08/06/java%E5%91%BD%E4%BB%A4/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">java命令</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/10/">October 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/08/">August 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/07/">July 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2020/04/">April 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2020/10/25/%E6%AF%8F%E6%97%A5%E5%AD%A6%E4%B9%A0/">每日学习</a>
          </li>
        
          <li>
            <a href="/blog/2020/10/12/mybatis-plus/">mybatis-plus</a>
          </li>
        
          <li>
            <a href="/blog/2020/08/30/%E7%A7%92%E6%9D%80%E7%B3%BB%E7%BB%9F/">秒杀系统</a>
          </li>
        
          <li>
            <a href="/blog/2020/08/29/elasticsearch%E7%9F%A5%E8%AF%86/">elasticsearch知识</a>
          </li>
        
          <li>
            <a href="/blog/2020/08/28/redis%E9%9D%A2%E8%AF%95/">redis面试</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 yanglf007<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">

  
<script src="/blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/blog/js/script.js"></script>




  </div>
</body>
</html>